package com.Leetcode;

import com.Util.ProduceTree;
import com.structure.TreeNode;
public class Index654constructMaximumBinaryTree {
	
	public static void main(String[] args) {
		int[] nums = {3,2,1,6,0,5};
		ProduceTree.treeNodeToString(constructMaximumBinaryTree(nums));
	}
	public static TreeNode constructMaximumBinaryTree(int[] nums) {
		int high = nums.length - 1;
		int low = 0;
		TreeNode root = BinaryTree(nums,low,high);
		return root;
    }
	
	public static TreeNode BinaryTree(int[] nums,int low,int high){
		if(high < low) return null;
		int max = low;
		for(int i = low + 1;i <= high; i++)
			max = nums[max] > nums[i] ? max : i;
			
		TreeNode node = new TreeNode(nums[max]);
		node.left = BinaryTree(nums,low,max - 1);
		node.right = BinaryTree(nums,max + 1,high);
		return node;
	}
}
